package tt.dz.dao.pro;

import java.util.List;

import org.springframework.stereotype.Repository;

import tt.dz.dao.base.BaseDao;
import tt.dz.entity.UserMember;

/**
 * 用户信息操作Dao类
 * @author xielf
 *
 * @param <T>
 */
@Repository
@SuppressWarnings(value={"unchecked","rawtypes"})
public class UserMemberDao<T extends UserMember> extends BaseDao {

	/**
	 * 通过userid查找单个用户信息
	 * @param userid
	 * @return
	 */
	public T findUserMemberByUserId(Integer userid){
		List<T> list = super.findByHql("FROM UserMember where userid=?", userid);
		if(list!=null && list.size()>0){
			return list.get(0);
		}
		return null;
	}
	
	/**
	 * 通过多个userid查找用户列表信息
	 * @param userid
	 * @return
	 */
	public List<T> findUserMemberByUserIds(String userid){
		return super.findBySql("SELECT * from user_member WHERE userid IN ("+userid+")", UserMember.class);
	}

	/**
	 * 查询用户通过userid，token
	 * @param token
	 * @param userid
	 * @return
	 */
	public Boolean validUserToken(String token,String userid){
		List<T> members = super.findByHql("from UserMember where userid=? and token=?", userid, token);
		if(members!=null && members.size()>0){
			return true;
		}
		return false;
	}
	/**
	 * 查看用户名是否存在
	 * @param userName 用户名高
	 * @return
	 */
	public List<T> existsUserInfoByUserName(String userName){
		return super.findByHql("from UserMember where username=?", userName);
	}
	
	/**
	 * 查询最大的phpssouid
	 * @return
	 */
	public Integer findMax_phpssouid(){
		List<Integer> onlyOneMaxData = super.findBySql("SELECT max(phpssouid) FROM user_member",null);
		return onlyOneMaxData.get(0).intValue();
	}
	
	/**
	 * 分页查询用户信息
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public List<T> findUserMemberPaging(Integer pageNo,Integer pageSize){
		return super.findPagingByHql("from UserMember", pageNo, pageSize);
	}
	
	/**
	 * 查询附近用户
	 * @param lat
	 * @param lng
	 * @param pageNo
	 * @param pageSize
	 * @return
	 */
	public List<T> findNearUserMember(Double lat,Double lng,Integer pageNo,Integer pageSize){
		return super.findPagingBySql("select *, IFNULL(getDistance(lng,lat,?,?),0) as distance from user_member where lng is not null and lat is not null ORDER BY distance asc", UserMember.class, pageNo, pageSize, lng, lat);
	}
	
	/**
	 * 查询距离
	 * @param lat 纬度
	 * @param lng	经度
	 * @param userId	用户ID
	 * @return
	 */
	public Double findDistanceByUserIdAndLatLng(Double lat,Double lng,Integer userId){
		List<Double> doubles = super.findBySql("select IFNULL(getDistance(lng,lat,?,?),0) as distance from user_member where lng is not null and lat is not null and userid=?", null,lng,lat,userId);
		return doubles.get(0);
	}
	
	/**
	 * 通过手机号码查询用户信息
	 * @param phone
	 * @return
	 */
	public UserMember findUserMemberByPhone(String phone){
		
		List<UserMember> userMembers = super.findByHql("from UserMember where username=?", phone);
		if(userMembers!=null && userMembers.size()>0){
			return userMembers.get(0);
		}
		return null;
	}
	
	/**
	 * 通过im用户名查询用户名
	 * @param chatUserName im用户名
	 * @return
	 */
	public List<UserMember> findUserMemberByChatUserName(String chatUserName){

		chatUserName = chatUserName.replace("'", "\"");
		chatUserName = chatUserName.replace(",", "\",\"");
		chatUserName = "\""+chatUserName+"\"";
		System.out.println(chatUserName);
		List<UserMember> userMembers = super.findByHql("from UserMember where chatusername in ("+chatUserName+")");
		if(userMembers!=null && userMembers.size()>0){
			return userMembers;
		}
		return null;
	}
	
	/**
	 * 通过手机号码模糊查询用户信息
	 * @param phone
	 * @return
	 */
	public List<UserMember> findUserInfoFuzzyByPhone(String phone,int pageNo,int pageSize){
		
		List<UserMember> userMembers = super.findPagingByHql("from UserMember where username like ('"+phone+"%')",pageNo,pageSize);
		if(userMembers!=null && userMembers.size()>0){
			return userMembers;
		}
		return null;
	}
	
	/**
	 * 通过手机号码查询用户详细信息
	 * @param phone
	 * @return
	 */
	public UserMember findUserByPhone(String phone){
		
		List<UserMember> userMembers = super.findByHql("from UserMember where username = ?", phone);
		if(userMembers!=null && userMembers.size()>0){
			return userMembers.get(0);
		}
		return null;
	}
}
